// pages/index/law_service/reserve/reserve.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    time: null,
    phone: '',
    dateShow: false,
    //留言
    remark: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getPhoneNum()
  },
  /**
   * 选择日期
   * @param e
   */
  onSelectDate: function (e) {
    this.setData({
      dateShow: true
    })
  },

  /**
   * 日期选择关闭
   * @param e
   */
  onDateClose: function (e) {
    this.setData({
      dateShow: false
    })
  },

  /**
   * 日期选择确定
   * @param e
   */
  onDateConfirm: function (e) {
    this.setData({
      time: this.formatDate(e.detail),
      dateShow: false
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${(date.getMonth() + 1)<10?'0'+(date.getMonth() + 1):(date.getMonth() + 1)}-${date.getDate()<10?'0'+date.getDate():date.getDate()}`;
  },

  /**
   * 实时监听联系电话输入框
   * @param e
   */
  onPhoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  /**
   * 实时监听备注输入框
   * @param e
   */
  onRemarkInput: function (e){
    this.setData({
      remark: e.detail.value
    })
  },

  /**
   * 提交预约数据
   * @param e
   */
  onSubmitOrder: function (e) {
    const _this = this
    if (this.data.time == null || this.data.time.length <= 0) {
      Dialog.alert({
        title: "温馨提示",
        message: "请选择预约日期"
      }).then(() => {})
      return
    }
    if (this.data.phone.length <= 0) {
      Dialog.alert({
        title: "温馨提示",
        message: "请输入电话号码"
      }).then(() => {})
      return
    }
    if (this.data.phone.length < 11) {
      Dialog.alert({
        title: "温馨提示",
        message: "请输入正确的电话号码"
      }).then(() => {})
      return
    }
    Toast.loading({
      duration: 0,
      message: "正在提交"
    })
    http.postJson("attorney_record/add",{
      type: 2,
      applet_user_id: app.globalData.userInfo.applet_user_id,
      user_name: wx.getStorageSync("name"),
      zz_people_id: _this.data.id,
      area_code: app.globalData.areaCode,
      make_date: _this.data.time,
      remark: _this.data.remark,
      phone: wx.getStorageSync("phone")
    }).then(res => {
      Toast.clear()
      if (res.data.code === http.REQUEST_OK) {
        Dialog.alert({
          title: "预约律师",
          message: "预约成功"
        }).then(() => {
          wx.navigateBack()
        })
      }else {
        Dialog.alert({
          title: "预约失败",
          message: res.data.msg
        }).then(() => {})
      }
    }, err => {
      Toast.fail("网络异常")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取电话号码
   */
  getPhoneNum: function () {
    const than = this
    http.get("people_info/people", {
      people_id: app.globalData.userInfo.people_id,//'8a8080d373991a3b0173991a3bce0000'
    }).then((res) => {
      if (res.data.code != 200) {
        wx.getStorage({
          key: "phone",
          success: function (res) {
            if (res.data.length == 11) {
              than.setData({
                phone: res.data
              })
            } else {
              Dialog.confirm({
                title: '提示',
                message: '为了方便您的操作，请先绑定手机号',
                confirmButtonOpenType: "getPhoneNumber"
              }).then(() => {
                setTimeout(() => {
                  Dialog.close();
                }, 1000);
              }).catch(() => {
                Dialog.close();
              });
            }
          }
        })
      } else {
        if (res.data.data.phone.length <= 0) {
          wx.getStorage({
            key: "phone",
            complete: function (res) {
              log.e(JSON.stringify(res))
              if (res.errMsg != 'getStorage:fail data not found') {
                than.setData({
                  phone: res.data
                })
              } else {
                Dialog.confirm({
                  title: '提示',
                  message: '为了方便您的操作，请先绑定手机号',
                  confirmButtonOpenType: "getPhoneNumber"
                }).then(() => {
                  setTimeout(() => {
                    Dialog.close();
                  }, 1000);
                }).catch(() => {
                  Dialog.close();
                });
              }
            }
          })
        } else {
          than.setData({
            phone: res.data.data.phone
          })
        }
      }
    })
  },

  getPhoneNumber: function (e) {
    const than = this
    wx.login({
      success: function (r) {
        http.get("applet/user/wxDecryptPhone", {
              appid: app.globalData.appId,
              code: r.code,
              iv: e.detail.iv,
              encryptedData: e.detail.encryptedData,
            }
        ).then((res) => {
          if (res.data.code == 200) {
            wx.setStorage({
              key: "phone",  // 存储的key值
              data: res.data.data.phoneNumber   // 需要存储的值
            })
            than.setData({
              phone: res.data.data.phoneNumber
            })
          }
        })
      }
    })
  },
})